package com.ysqcould.web.controller.content;


import com.ysqcould.allocation.utils.UploadFileUtil;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.config.YsqConfig;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.file.FileUploadUtils;
import com.ysqcould.common.utils.poi.ExcelUtil;
import com.ysqcould.content.domain.SlideShow;
import com.ysqcould.content.service.ISlideShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * 轮播图Controller
 * 
 * @author ruoyi
 * @date 2022-06-28
 */
@RestController
@RequestMapping("/content/show")
public class SlideShowController extends BaseController
{
    @Autowired
    private ISlideShowService tSlideShowService;

    /**
     * 查询轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('content:show:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlideShow slideShow)
    {
        startPage();
        List<SlideShow> list = tSlideShowService.selectTSlideShowList(slideShow);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('content:show:export')")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlideShow slideShow)
    {
        List<SlideShow> list = tSlideShowService.selectTSlideShowList(slideShow);
        ExcelUtil<SlideShow> util = new ExcelUtil<SlideShow>(SlideShow.class);
        util.exportExcel(response, list, "轮播图数据");
    }

    /**
     * 获取轮播图详细信息
     */
    @PreAuthorize("@ss.hasPermi('content:show:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tSlideShowService.selectTSlideShowById(id));
    }

    /**
     * 新增轮播图
     */
    @PreAuthorize("@ss.hasPermi('content:show:add')")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlideShow slideShow)
    {
        slideShow.setAsId(getUserId());
        return toAjax(tSlideShowService.insertTSlideShow(slideShow));
    }

    /**
     * 修改轮播图
     */
    @PreAuthorize("@ss.hasPermi('content:show:edit')")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlideShow slideShow
)
    {
        return toAjax(tSlideShowService.updateTSlideShow(slideShow));
    }

    /**
     * 删除轮播图
     */
    @PreAuthorize("@ss.hasPermi('content:show:remove')")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSlideShowService.deleteTSlideShowByIds(ids));
    }

    /**
     * 剪切之后的轮播图上传
     */
    @Log(title = "轮播图上传", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(YsqConfig.getAvatarPath(), file);
            final String imageUrl = avatar.substring(avatar.indexOf(".") + 1);
            final String uuid = UUID.randomUUID().toString().replace("-", "");
            String newUrl=uuid+"."+imageUrl;
            final InputStream inputStream = file.getInputStream();
            final String upload = UploadFileUtil.AliOSSUpload(inputStream, newUrl);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", upload);
            return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
